/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author VuTienIT
 */
public class KhuVucDAO {
    public static ArrayList<KhuVucPOJO> getDanhSach()
    {
        ArrayList<KhuVucPOJO> s = new ArrayList<KhuVucPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From KhuVuc";
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                KhuVucPOJO temp = new KhuVucPOJO();
                temp.setMaKhuVuc(rs.getInt("MaKhuVuc"));
                temp.setTenKhuVuc(rs.getString("TenKhuVuc"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(KhachHangPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static ArrayList<KhuVucPOJO> getDanhSachLoaiTru(int maKV)
    {
        ArrayList<KhuVucPOJO> s = new ArrayList<KhuVucPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From KhuVuc Where MaKhuVuc!="+maKV;
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                KhuVucPOJO temp = new KhuVucPOJO();
                temp.setMaKhuVuc(rs.getInt("MaKhuVuc"));
                temp.setTenKhuVuc(rs.getString("TenKhuVuc"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(KhachHangPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static KhuVucPOJO getKhuVuc(int maQ)
    {
        KhuVucPOJO temp = new KhuVucPOJO();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = String.format("Select * From KhuVuc Where MaKhuVuc=%d",maQ);
            ResultSet rs = helper.executeQuery(sql);
            if(rs.next())
            {
                temp.setMaKhuVuc(rs.getInt("MaKhuVuc"));
                temp.setTenKhuVuc(rs.getString("TenKhuVuc"));
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(KhuVucDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return temp;
    }
}
